package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.Page;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.BorrowTenderHisDao;
import com.qdlc.p2p.dal.dto.BorrowTenderHis;
import com.qdlc.p2p.dal.model.BorrowTenderHisModel;
import com.qdlc.p2p.dal.model.BorrowTenderModel;

@Repository("borrowTenderHisDao")
public class BorrowTenderHisDaoImpl extends BaseDaoImpl<BorrowTenderHis> implements BorrowTenderHisDao {

    @SuppressWarnings("unchecked")
    @Override
    public PageDataList<BorrowTenderHisModel> getTenderRecordlist(BorrowTenderModel model) {
        StringBuffer sb = new StringBuffer("");
        sb.append(" select title,scales,apr,money,interest,status from " + Global.DB_PREFIX + "borrow_tender_his WHERE user_id=:userId");

        Date d = DateUtil.getDate(System.currentTimeMillis() / 1000 + "");
        Date startTime = null;
        Date endTime = null;
        if (model.getTime() == 7) {
            sb.append(" AND add_time >= :startTime");
            sb.append(" AND add_time <= :endTime");
            startTime = DateUtil.rollDay(d, -7);
            endTime = d;
        } else if (model.getTime() > 0 && model.getTime() < 4) {
            sb.append(" AND add_time >= :startTime");
            sb.append(" AND add_time <= :endTime");
            startTime = DateUtil.rollMon(d, -model.getTime());
            endTime = d;
        }
        if (StringUtil.isNotBlank(model.getStartTime())) {
            startTime = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
            sb.append(" AND add_time > :startTime");
        }
        if (StringUtil.isNotBlank(model.getEndTime())) {
            endTime = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
            sb.append(" AND add_time < :endTime");
        }
        sb.append(" ORDER BY add_time DESC  ");

        Query query = em.createNativeQuery(sb.toString());
        query.setParameter("userId", model.getUser().getUserId());
        if (startTime != null) {
            query.setParameter("startTime", startTime);
        }
        if (endTime != null) {
            query.setParameter("endTime", endTime);
        }
        Page page = new Page(query.getResultList().size(), model.getPage(), model.getSize());
        query.setFirstResult((model.getPage() - 1) * model.getSize());
        query.setMaxResults(model.getSize());
        List<Object[]> list = query.getResultList();
        List<BorrowTenderHisModel> modelList = new ArrayList<BorrowTenderHisModel>();
        PageDataList<BorrowTenderHisModel> pageDataList = new PageDataList<BorrowTenderHisModel>();
        for (Object[] o : list) {
            BorrowTenderHisModel bt = new BorrowTenderHisModel();
            bt.setTitle(o[0] + "");
            bt.setScales(Double.parseDouble((o[1] + "")));
            bt.setApr(Double.parseDouble((o[2] + "")));
            bt.setMoney(Double.parseDouble((o[3] + "")));
            bt.setInterest(Double.parseDouble((o[4] + "")));
            bt.setStatus((Integer) o[5]);
            modelList.add(bt);
        }

        pageDataList.setList(modelList);
        pageDataList.setPage(page);
        return pageDataList;
    }



}
